﻿<?php
/**
 * @author 
 * @copyright 2014
 */
 
 
class TrudiemtudongModel extends CI_Model
{
    var $_menus = array();
	function __construct(){
		parent::__construct('');
		$this->load->model();
		$this->load->model(array('smsemail_model')); //$this->lang->load('title');
	}
	function trudiemQuahan(){
		$sql = "
			SELECT b.id as idklgbcha,a.id as idklgb,   a.idphongban , a.madonvi , a.tendonvi, b.chamdiemdc,b.chamdiem as diemcham, a.ngayhoanthanh, b.chamdiem -0.5 as diemtru , c.dsdienthoai, c.dsemail ,a.noidung
				FROM `klphongban` as a 
				join  klgb as b on (a.idkl = b.id) 
				join  smsemail as c on (a.idphongban = c.iddonvi)
				Where DATEDIFF( a.ngayhoanthanh, DATE( NOW() )) < 0 and b.isdelete = 0 and a.isdelete = 0 and  b.chamdiem > -1.5 and a.types =1
			";
		$query = $this->model->query($sql)->execute();
		$arrConftig = $this->getConfig();
		$arrPhongban = $this->getPhongban();
		if(isset($arrConftig['diemtru_quahan'])){
			$diemtru_quahan = $arrConftig['diemtru_quahan'];
		}
		else{
			$diemtru_quahan = -0.5;
		}
		$sql_insert = '
			INSERT INTO `chamdiem` (
							`idklgbcha` ,				
							`idklgb` ,				
							`idphongban` ,
							`madonvi` ,
							`tendonvi` ,
							`diem` ,
							`loai` ,
							`ghichu` ,
							`usercreate` , 
							`datecreate`
						)
						VALUES ';
		$usercreate = "trừ điểm tự động";
		$datecreate = gmdate("Y-m-d H:i:s",time()+7*3600);
		$ghichu = "Qua han";
		$i=0;
		foreach($query as $item){
			if(isset($arrPhongban[$item->idphongban])){
				$arr_pb = explode("__",$arrPhongban[$item->idphongban]);
				$sql_insert.= "(";
				$sql_insert.= "'".($item->idklgbcha)."',";
				$sql_insert.= "'".($item->idklgb)."',";
				$sql_insert.= "'".($item->idphongban)."',";
				$sql_insert.= "'".$arr_pb[0]."',";
				$sql_insert.= "'".$arr_pb[1]."',";
				$sql_insert.= "'".$diemtru_quahan."',";
				$sql_insert.= "'0',";
				$sql_insert.= "'".$ghichu."',";
				$sql_insert.= "'".$usercreate."','".$datecreate."'),\n";	
				$i++;
				$this->model->table('klgb')->where('id',$item->idklgbcha)->update(array('chamdiem'=>$item->diemtru)) ;
				$noidungnhantin ="Den 17h gio ngay hom nay he thong da tru -0.5 diem do user". $item->madonvi." da de qua han KLGB ma " .$item->idklgbcha . " noi dung: " . $item->noidung. " Chi tiet vui long vao PM cham diem Chi nhanh xem hoac lien he chuyen quan";
				$this->smsemail_model->Sendsms($item->dsdienthoai,$noidungnhantin);
			}
		}
		if($i > 0){
			$sql_insert = substr($sql_insert,0,-2).";";
		    $this->model->query($sql_insert)->execute();
			
			return "ok";
		}
		else{
			return "nok";
		}
	}
	function trudiemTronghan(){
		/* Còn 3 ngày nữa hoàn thành*/
		$sql = "
			SELECT b.id as idklgbcha,a.id as idklgb,   a.idphongban , a.madonvi , a.tendonvi, b.chamdiemdc,b.chamdiem as diemcham, a.ngayhoanthanh, b.chamdiem -0.5 as diemtru , c.dsdienthoai, c.dsemail ,a.noidung
				FROM `klphongban` as a 
				join  klgb as b on (a.idkl = b.id) 
				join  smsemail as c on (a.idphongban = c.iddonvi)
				Where  DATEDIFF( a.ngayhoanthanh, DATE( NOW() )) <= 3 AND DATEDIFF( a.ngayhoanthanh, DATE( NOW( ) )) >= 0 and b.isdelete = 0 and a.isdelete = 0 and  b.chamdiem > -1.5 and a.types =1
				and a.id not in (select idklcon from traketqua as d where date(d.datecreate) = date(now()))
		";
		$query = $this->model->query($sql)->execute();
		$arrConftig = $this->getConfig();
		$arrPhongban = $this->getPhongban();
		if(isset($arrConftig['diemtru_saphoanthanh'])){
			$diemtru_saphoanthanh = $arrConftig['diemtru_saphoanthanh'];
		}
		else{
			$diemtru_saphoanthanh = 0.5;
		}
		$sql_insert = '
			INSERT INTO `chamdiem` (
							`idklgb` ,				
							`idphongban` ,
							`madonvi` ,
							`tendonvi` ,
							`diem` ,
							`loai` ,
							`ghichu` ,
							`usercreate` , 
							`datecreate`
						)
						VALUES ';
		$usercreate = "CĐTĐ";
		$datecreate = gmdate("Y-m-d H:i:s",time()+7*3600);
		$ghichu = "Sap hoan thanh";
		$i=0;
		foreach($query as $item){
			if(isset($arrPhongban[$item->idphongban])){
				$arr_pb = explode("__",$arrPhongban[$item->idphongban]);
				$sql_insert.= "(";
				$sql_insert.= "'".($item->id)."',";
				$sql_insert.= "'".($item->idphongban)."',";
				$sql_insert.= "'".$arr_pb[0]."',";
				$sql_insert.= "'".$arr_pb[1]."',";
				$sql_insert.= "'".$diemtru_saphoanthanh."',";
				$sql_insert.= "'0',";
				$sql_insert.= "'".$ghichu."',";
				$sql_insert.= "'".$usercreate."','".$datecreate."'),\n";	
				$i++;
			}
		}
		if($i > 0){
			$sql_insert = substr($sql_insert,0,-2).";";
		    $this->model->query($sql_insert)->execute();
			return "ok";
		}
		else{
			return "ok";
		}
	}
	function trudiemThuongxuyen(){
	
	}
	function getConfig(){
		$config = $this->model->table('config')
					   ->find_combo('config_name','config_value');
		return $config;
	}
	function getPhongban(){
		$phongban = $this->model->table('department')
							    ->select('iduser,codes,names')
								->where('isdelete',0)
								->find_all();
		$arr = array();
		foreach($phongban as $item){
			$arr[$item->iduser] = ($item->codes)."__".($item->names);
		}
		return $arr; 
	}
}	